package com.geek.config.standard;

import javax.management.*;
import java.io.IOException;
import java.lang.management.ManagementFactory;

/**
 * @Author: ysq
 * @Date: 2021/7/20
 * @Description:  标准MBean注册 demo cmd -> jconsole 建立连接
 */
public class Client {
  public static void main(String[] args) throws MalformedObjectNameException, InstanceAlreadyExistsException
          , MBeanRegistrationException, NotCompliantMBeanException, IOException {

    final User user = new User();
    registryMBean(UserMBean.class,user);
    registryMBean(UserMXBean.class,user);
    registryMBean(UserMXBeanAnnotation.class,user);

    System.out.println(" Press any key to exit ");
    System.in.read();
  }

  private static void registryMBean(final Class mBeanInterface, final User user) throws MalformedObjectNameException,
          InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException{

    final String packageName = mBeanInterface.getPackage().getName();
    final String simpleName = mBeanInterface.getSimpleName();
    final ObjectName objectName = new ObjectName(packageName + ":type=" + simpleName);
    final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    final ObjectInstance mBean = server.registerMBean(user, objectName);
  }
}
